package com.drought.rained.ui.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Vibrator;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.drought.rained.R;
import com.drought.rained.LJApplication;
import com.drought.rained.base.BaseTitleActivity;
import com.drought.rained.business.RouterMapLJ;
import com.drought.rained.cache.AcacheLJ;
import com.drought.rained.cache.AcacheKeysLJ;
import com.drought.rained.common.ConstantLJ;
import com.drought.rained.common.RouterCommandLJ;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.drought.rained.common.RouterCommandLJ.STATUS_TYPE_KEY;

@Route(name = "状态展示页", path = RouterMapLJ.STATUS_DISPLAY)
public class StatusActivityLJ extends BaseTitleActivity {

    @BindView(R.id.text_status_content)
    TextView text_status_content; // 内容
    @BindView(R.id.btn_operate)
    TextView btn_operate; // 按钮

    private String mPageTitle = "";
    private String mStatusContent;
    private String mStatusOpText;

    private int pageType;//0:认证结果 1:借款结果 2:ATM还款 3:OTC还款
    private CountDownTimer countDownTimer;

    @Override
    protected void initViews() {
        ButterKnife.bind(this);
    }

    @Override
    protected void onUIReady() {
        if (pageType == ConstantLJ.STATUS_TYPE_AUTH) {
            setTitleText(getStringResource(R.string.status_auth_page_title));
            text_status_content.setText(getResources().getString(R.string.status_auth_tip));
            btn_operate.setText(getResources().getString(R.string.status_jump_to_main));
        } else if (pageType == ConstantLJ.STATUS_TYPE_BORROW) {
            setTitleText(getStringResource(R.string.status_borrow_page_title));
            text_status_content.setText(getResources().getString(R.string.status_borrow_tip));
            btn_operate.setText(getResources().getString(R.string.status_jump_to_bill));
            Vibrator vibrator = (Vibrator)this.getSystemService(VIBRATOR_SERVICE);
            vibrator.vibrate(1000);
        }

        countDownTimer = new CountDownTimer(5 * 1000, 1000) {

            @SuppressLint("SetTextI18n")
            @Override
            public void onTick(long millisUntilFinished) {
                btn_operate.setText(getStringResource(R.string.status_jump_to_main) + " (" +String.valueOf(millisUntilFinished / 1000) + "s)");
            }

            @Override
            public void onFinish() {
                jumpMain();
            }
        };
        countDownTimer.start();
    }

    @OnClick(R.id.btn_operate)
    public void onClick() {
        jumpMain();
    }

    private void jumpMain() {
        if (countDownTimer != null) {
            countDownTimer.cancel();
        }
        switch (pageType) {
            case ConstantLJ.STATUS_TYPE_AUTH:
                ARouter.getInstance().build(RouterMapLJ.MAIN_ACTIVITY_PAGE).navigation();
                break;
            case ConstantLJ.STATUS_TYPE_BORROW:
                AcacheLJ.get(LJApplication.getContext()).put(AcacheKeysLJ.LICENSE, true);
                ARouter.getInstance().build(RouterMapLJ.MINE_BILL).withInt(RouterCommandLJ.KEY_OF_BOTTOM_BAR_INDEX,1).navigation();
                break;
        }
        finish();
    }

    @Override
    protected int getContentResId() {
        return R.layout.layout_status_content;
    }

    @Override
    public String getPageTitle() {
        if (getIntent() != null && getIntent().getExtras() != null) {
            Bundle extras = getIntent().getExtras();
            pageType = extras.getInt(STATUS_TYPE_KEY, 0);
        }
        getConfigurationPage(); // 获取页面配置

        return mPageTitle;
    }

    private void getConfigurationPage() {
        switch (pageType) {
            case ConstantLJ.STATUS_TYPE_AUTH:
                mPageTitle = getResources().getString(R.string.status_auth_page_title);
                mStatusContent = getResources().getString(R.string.status_auth_tip);
                mStatusOpText = getResources().getString(R.string.status_jump_to_main);
                break;
            case ConstantLJ.STATUS_TYPE_BORROW:
                mPageTitle = getResources().getString(R.string.status_borrow_page_title);
                mStatusContent = getResources().getString(R.string.status_borrow_tip);
                mStatusOpText = getResources().getString(R.string.status_jump_to_bill);
                break;
        }
    }

}
